package br.com.iswe.recipemaster.gwt.recipe.client;

import br.com.iswe.gwt.ui.client.QueryInfo;
import br.com.iswe.gwt.ui.client.RPCService;
import br.com.iswe.gwt.ui.client.RequestInfo;
import br.com.iswe.recipemaster.gwt.client.RmDetailPanel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RecipeDetailPanel extends RmDetailPanel {
	
	private Button closeButton;
	private Button saveButton;
	private Button deleteButton;
	
	// ## IMPORTANT :: -->> This queryInfo must contain the properties needed to populate the screen (during detail)
	protected QueryInfo queryInfoDetail = new QueryInfo(new String[]{ getIdProperty(), RecipeConstants.NAME, RecipeConstants.DESCRIPTION, VERSION });
	
	protected RequestInfo requestInfoSave = new RequestInfo(new String[]{ getIdProperty(), VERSION });
	
	protected RPCService service = new RPCService(RecipeConstants.CONTROLLER_NAME);
	private TextBox recipeIdTextBox;
	private Hyperlink addIngredientsHyperlink;
	private Hyperlink deleteSelectedIngredientsHyperlink;
	private TextBox nameTextBox;
	private TextArea descriptionTextArea;
	
	
	public RecipeDetailPanel() {
		final VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setHeight("360px");
		initWidget(verticalPanel);
		verticalPanel.setSpacing(4);
		
		final AbsolutePanel absolutePanel = new AbsolutePanel();
		verticalPanel.add(absolutePanel);
		absolutePanel.setSize("540px", "180px");

		saveButton = new Button();
		absolutePanel.add(saveButton, 268, 11);
		saveButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				save();
			}
		});
		saveButton.setWidth("75px");
		saveButton.setText("Save");

		closeButton = new Button();
		absolutePanel.add(closeButton, 452, 11);
		closeButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				cancelClose();
			}
		});
		closeButton.setWidth("75");
		closeButton.setText("Close");

		deleteButton = new Button();
		absolutePanel.add(deleteButton, 359, 11);
		deleteButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				delete();
			}
		});
		deleteButton.setWidth("75");
		deleteButton.setText("Delete");
		
		Label lblRecipeId = new Label("Recipe ID : ");
		absolutePanel.add(lblRecipeId, 5, 22);
		
		recipeIdTextBox = new TextBox();
		recipeIdTextBox.setReadOnly(true);
		absolutePanel.add(recipeIdTextBox, 100, 22);
		recipeIdTextBox.setSize("132px", "22px");
		
		Label lblName = new Label("Name : ");
		absolutePanel.add(lblName, 5, 66);
		
		nameTextBox = new TextBox();
		absolutePanel.add(nameTextBox, 100, 65);
		nameTextBox.setSize("334px", "22px");
		
		Label lblDescription = new Label("Description : ");
		absolutePanel.add(lblDescription, 5, 106);
		
		descriptionTextArea = new TextArea();
		absolutePanel.add(descriptionTextArea, 100, 106);
		descriptionTextArea.setSize("334px", "63px");
		
		AbsolutePanel ingredientsPanel = new AbsolutePanel();
		verticalPanel.add(ingredientsPanel);
		ingredientsPanel.setSize("540", "170");
		
		Label lblIngredients = new Label("Ingredients : ");
		ingredientsPanel.add(lblIngredients, 5, 0);
		
		deleteSelectedIngredientsHyperlink = new Hyperlink("Delete selected Ingredients", false, "newHistoryToken");
		ingredientsPanel.add(deleteSelectedIngredientsHyperlink, 274, 0);
		
		addIngredientsHyperlink = new Hyperlink("Add", false, "newHistoryToken");
		ingredientsPanel.add(addIngredientsHyperlink, 492, 0);
		
		ScrollPanel scrollPanel = new ScrollPanel();
		ingredientsPanel.add(scrollPanel, 0, 26);
		scrollPanel.setSize("540px", "144px");

		

		//registra os componentes na formFactory
		setupScreen();
	}
	
	protected void setupScreen() {
		//hidden properties
		//formFactory.buildProperty(VERSION);
		formFactory.addProperty(RecipeConstants.ID_PROPERTY, recipeIdTextBox);
		formFactory.addProperty(RecipeConstants.NAME, nameTextBox);
		formFactory.addProperty(RecipeConstants.DESCRIPTION, descriptionTextArea);
		
		//configura os componentes visuais (campos) na form factory para manilupar esses dados
		//formFactory.addProperty(ROLE_TYPE, roleTypeListBox, ROLE_TYPE_CONTROLLER, ROLE_TYPE_ID, DESCRIPTION, new RequiredValidator("Role Type"));
		
		//formFactory.loadListBoxes(service);
	}
	
	protected String getIdProperty() {
		return RecipeConstants.ID_PROPERTY;
	}
	
	protected RPCService getService(){
		return service;
	}
	
	protected RequestInfo getRequestInfoSave() {
		return requestInfoSave;
	}
	
	protected QueryInfo getQueryInfoDetail() {
		return queryInfoDetail;
	}
	
	protected FocusWidget getSaveButton() {
		return saveButton;
	}
	
	protected FocusWidget getDeleteButton() {
		return deleteButton;
	}
}
